package xcmg.device.service.threedayplan;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.delivery.DeliveryNoteDO;
import xcmg.device.dao.entity.threeGuaranteesPur.ThreeGuaranteesOutDO;
import xcmg.device.dao.entity.threedayplan.ThreeDayPlanDO;
import xcmg.device.dao.entity.threedayplan.ThreeDayPlanDetailDO;
import xcmg.device.dao.mapper.delivery.DeliveryNoteMapper;
import xcmg.device.dao.mapper.threeGuaranteesPur.ThreeGuaranteesOutMapper;
import xcmg.device.dao.mapper.threedayplan.ThreeDayPlanDOMapper;
import xcmg.device.dao.mapper.threedayplan.ThreeDayPlanDetailDOMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.vo.delivery.DeliveryNoteVO;
import xcmg.device.service.vo.delivery.OutBillValuesVO;
import xcmg.device.service.vo.threeGuaranteesPur.ThreeGuaranteesOutVO;
import xcmg.device.service.vo.threedayplan.DlvInfoQueryVO;
import xcmg.device.service.vo.threedayplan.ThreeDayPlanDetailVO;
import xcmg.device.service.vo.threedayplan.ThreeDayPlanQueryVO;
import xcmg.device.service.vo.threedayplan.ThreeDayPlanResultVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/8/10.
 * 三日计划业务处理
 */
@Service
public class ThreeDayPlanService {
    @Autowired
    private ThreeDayPlanDOMapper threeDayPlanDOMapper;

    @Autowired
    private ThreeDayPlanDetailDOMapper threeDayPlanDetailDOMapper;
    @Autowired
    private FastGenClient genClient;
    @Autowired
    private DeliveryNoteMapper deliveryNoteMapper;
    @Autowired
    private ThreeGuaranteesOutMapper threeGuaranteesOutMapper;

    public ErrorCode add(ThreeDayPlanResultVO threeDayPlanResultVO) throws Exception {
        ActionResult<String> actionResult = genClient.newGuidText();
        if (ErrorCode.Success.getCode() != actionResult.getCode()) {
            return ErrorCode.Failure;
        }

        String mainId = actionResult.getValue();//主表id
        List<ThreeDayPlanDetailVO> detailVOList = threeDayPlanResultVO.getDetailVOList();
        for (ThreeDayPlanDetailVO threeDayPlanDetailVO : detailVOList) {
            String detailId = genClient.newGuidText().getValue();
            if (StringUtils.isBlank(detailId)) {
                throw new Exception("详情信息主键生成失败");
            }

            ThreeDayPlanDetailDO record = new ThreeDayPlanDetailDO();
            BeanUtils.copyProperties(threeDayPlanDetailVO, record);
            record.setAgentId(threeDayPlanResultVO.getAgentId());
            record.setAgentName(threeDayPlanResultVO.getAgentName());
            record.setPurchaseLevel(0);
            record.setId(detailId);
            record.setMainId(mainId);
            record.setOrderNo(threeDayPlanDetailVO.getDeliveryNo());
            record.setCreateDate(threeDayPlanResultVO.getCreateDate());
            record.setCreateId(threeDayPlanResultVO.getCreateId());
            record.setCreateName(threeDayPlanResultVO.getCreateName());
            threeDayPlanDetailDOMapper.insertSelective(record);
        }

        ThreeDayPlanDO threeDayPlanDO = new ThreeDayPlanDO();
        BeanUtils.copyProperties(threeDayPlanResultVO, threeDayPlanDO);
        threeDayPlanDO.setId(mainId);
        threeDayPlanDOMapper.insertSelective(threeDayPlanDO);
        return ErrorCode.Success;
    }

    public ErrorCode del(List<String> ids) throws Exception {
        for (String id : ids) {
            ThreeDayPlanDO threeDayPlanDO = threeDayPlanDOMapper.selectByPrimaryKey(id);
            Integer isDlv = threeDayPlanDO.getIsDlv();
            if (isDlv != 0) {
                throw new Exception("已创建发运交接单不允许删除！");
            }
            threeDayPlanDO.setIsDel(1);
            threeDayPlanDOMapper.updateByPrimaryKeySelective(threeDayPlanDO);
        }

        return ErrorCode.Success;
    }

    public ErrorCode update(ThreeDayPlanResultVO threeDayPlanResultVO, String userId, String userName) throws Exception {
        String mainId = threeDayPlanResultVO.getId();
        ThreeDayPlanDO threeDayPlanDO = threeDayPlanDOMapper.selectByPrimaryKey(mainId);
        Integer isDlv = threeDayPlanDO.getIsDlv();
        if (isDlv != 0) {
            throw new Exception("已创建发运交接单不允许修改！");
        }

        threeDayPlanDetailDOMapper.deleteByMainId(mainId);
        Date date = new Date();
        List<ThreeDayPlanDetailVO> detailVOList = threeDayPlanResultVO.getDetailVOList();
        for (ThreeDayPlanDetailVO threeDayPlanDetailVO : detailVOList) {
            String detailId = genClient.newGuidText().getValue();
            if (StringUtils.isBlank(detailId)) {
                throw new Exception("详情信息主键生成失败");
            }

            ThreeDayPlanDetailDO record = new ThreeDayPlanDetailDO();
            BeanUtils.copyProperties(threeDayPlanDetailVO, record);
            record.setAgentId(threeDayPlanResultVO.getAgentId());
            record.setAgentName(threeDayPlanResultVO.getAgentName());
            record.setPurchaseLevel(0);
            record.setId(detailId);
            record.setMainId(mainId);
            record.setOrderNo(threeDayPlanDetailVO.getDeliveryNo());
            record.setCreateDate(date);
            record.setCreateId(userId);
            record.setCreateName(userName);
            threeDayPlanDetailDOMapper.insertSelective(record);
        }

        ThreeDayPlanDO record = new ThreeDayPlanDO();
        BeanUtils.copyProperties(threeDayPlanResultVO, record);
        threeDayPlanDOMapper.updateByPrimaryKeySelective(record);
        return ErrorCode.Success;
    }

    public PageCommonVO list(SearchCommonVO<ThreeDayPlanQueryVO> cond) {
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(cond.getPageNum(), cond.getPageSize());
        List<ThreeDayPlanDO> list = threeDayPlanDOMapper.list(cond.getFilters());
        List<ThreeDayPlanResultVO> resultVOList = new ArrayList<>();
        for (ThreeDayPlanDO threeDayPlanDO : list) {
            ThreeDayPlanResultVO resultVO = new ThreeDayPlanResultVO();
            BeanUtils.copyProperties(threeDayPlanDO, resultVO);
            resultVOList.add(resultVO);
        }
        PageCommonVO result = new PageCommonVO();
        result.setPageInfo(new PageInfo(list));
        result.setPageInfoList(resultVOList);
        return result;
    }

    public List<ThreeDayPlanDetailVO> detail(String mainId) {
        List<ThreeDayPlanDetailVO> voList = new ArrayList<>();
        List<ThreeDayPlanDetailDO> doList = threeDayPlanDetailDOMapper.selectByMainId(mainId);
        doList.forEach(x -> {
            ThreeDayPlanDetailVO threeDayPlanDetailVO = new ThreeDayPlanDetailVO();
            BeanUtils.copyProperties(x, threeDayPlanDetailVO);
            threeDayPlanDetailVO.setPurchaseOrgName(x.getAgentName());
            threeDayPlanDetailVO.setDeliveryNo(x.getOrderNo());
            threeDayPlanDetailVO.setPurchaseLevel("月度");
            threeDayPlanDetailVO.setPurchaseOrgId(x.getAgentId());
            voList.add(threeDayPlanDetailVO);
        });
        return voList;
    }

    public PageCommonVO getAddList(SearchCommonVO<DlvInfoQueryVO> cond) {
        PageCommonVO result = new PageCommonVO();
        String orderType = cond.getFilters().getOrderType();
        if (BillDesc.DeliveryNo.getDesc().equals(orderType)) {
            result = deliveryNote(cond);

        } else if (BillDesc.ThreeOut.getDesc().equals(orderType)) {
            result = threeOut(cond);

        }

        return result;
    }


    private PageCommonVO deliveryNote(SearchCommonVO<DlvInfoQueryVO> cond) {
        DeliveryNoteVO deliveryNoteVO = new DeliveryNoteVO();
        DlvInfoQueryVO dlvInfoQueryVO = cond.getFilters();
        deliveryNoteVO.setKeyWords(dlvInfoQueryVO.getKeyWords());
        deliveryNoteVO.setPurchaseOrgId(dlvInfoQueryVO.getAgentOrgId());
        deliveryNoteVO.setSupplierOrgId(dlvInfoQueryVO.getOrgId());
        deliveryNoteVO.setIsMonth(1);
        PageCommonVO result = new PageCommonVO();
        PageHelperPlus.orderBy("purchase_org_id asc, create_date desc");
        PageHelperPlus.startPage(cond.getPageNum(), cond.getPageSize());
        List<DeliveryNoteDO> doList = deliveryNoteMapper.list(deliveryNoteVO);
        List<OutBillValuesVO> voList = new ArrayList<>();
        for (DeliveryNoteDO entity : doList) {
            OutBillValuesVO model = new OutBillValuesVO();
            model.setBillType("XGFT");
            model.setCreateDate(entity.getCreateDate());
            model.setCreateName(entity.getCreateName());
            model.setCreatorId(entity.getCreatorId());
            model.setDeliveryNo(entity.getDeliveryNo());
            model.setId(entity.getId());
            model.setPurchaseNo(entity.getPurchaseNo());
            model.setPurchaseOrgId(entity.getPurchaseOrgId());
            model.setPurchaseOrgName(entity.getPurchaseOrgName());
            model.setSaleNo(entity.getSaleNo());
            model.setNote(entity.getNote());
            model.setPurchaseLevel("月度");
            voList.add(model);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }

    private PageCommonVO threeOut(SearchCommonVO<DlvInfoQueryVO> cond) {
        ThreeGuaranteesOutVO condition = new ThreeGuaranteesOutVO();
        condition.setOrgId(cond.getFilters().getOrgId());
        condition.setKeyWords(cond.getFilters().getKeyWords());
        condition.setPurchaseOrgId(cond.getFilters().getAgentOrgId());
        PageCommonVO result = new PageCommonVO();
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(cond.getPageNum(), cond.getPageSize());
        List<ThreeGuaranteesOutDO> doList = threeGuaranteesOutMapper.list(condition);
        List<OutBillValuesVO> voList = new ArrayList<>();
        for (ThreeGuaranteesOutDO entity : doList) {
            OutBillValuesVO model = new OutBillValuesVO();
            model.setBillType("XGSC");
            model.setCreateDate(entity.getCreateDate());
            model.setCreateName(entity.getCreateName());
            model.setCreatorId(entity.getCreateId());
            model.setDeliveryNo(entity.getGuaranteesOutNo());
            model.setId(entity.getId());
            //model.setPurchaseNo(entity.getPurchaseNo());
            model.setPurchaseOrgId(entity.getPurchaseOrgId());
            model.setPurchaseOrgName(entity.getPurchaseOrgName());
            model.setSaleNo(entity.getPurchaseNo());
            model.setNote(entity.getNote());
            model.setPurchaseLevel("月度");
            voList.add(model);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }
}
